--  ---------------------------------------------------------------------------
--  @File: code_parser.ads
--
--  @Project: LiberAda
--  @Version: 1.0
--  @Created: 2009-10-19
--  @Author:  Ivan Bogicevic, Felix Krause
--
--  @Description:
--    The parser analyses every given token. Depending on the history of former
--    tokens, a symbol is generated out of a token (e.g. the tokens "Ada", ".",
--    "Text_IO" in that order lead to a symbol "Ada.Text_IO").
--
--  @Modifications:
--    +2009-10-19 IB: Initial release
--    +2011-04-06 FK: Removed unneccessary with clauses
with Ada.Strings.Unbounded;

with Ada_Tokenizer;

with Symbol;

package Code_Parser is

   package ASU renames Ada.Strings.Unbounded;

   --  ------------------------------------------------------------------------
   --  @Procedure: Set_Filepath
   --  @Description:
   --    The path including filename to the ada source file to be parsed can be
   --    set here.
   --  @Parameter:
   --    +Filepath: The path including filename of the ada source file
   procedure Set_Filepath (Filepath : ASU.Unbounded_String);

   --  ------------------------------------------------------------------------
   --  @Procedure: Clear
   --  @Description: Resets the current state of the parser to initial values.
   procedure Clear;

   --  ------------------------------------------------------------------------
   --  @Function: Get_Current_Context
   --  @Description:
   --    Determines the current context, which is the filename and the current
   --    package
   --  @Return:
   --    The current context of the last token
   function Get_Current_Context return ASU.Unbounded_String;

   --  ------------------------------------------------------------------------
   --  @Function: Token_To_Symbol
   --  @Description:
   --    Converts a token to a symbol. If no symbol could be generated (e.g.
   --    when not all information about a symbol are collected yet), an empty
   --    symbol is returned.
   --  @Parameter:
   --    +Kind: The kind of the token (e.g. DOT_T)
   --    +Value: The string value of the token as in the source code (e.g. ".")
   --  @Return:
   --    The symbol to the given token (empty if no symbol could be generated)
   function Token_To_Symbol
     (Kind   : in Ada_Tokenizer.Token_Type;
      Value  : in ASU.Unbounded_String)
      return Symbol.Symbol_Type;

   --  ------------------------------------------------------------------------
   --  @Function: Unify_Line_Ends
   --  @Description:
   --    Unifies the different line endings CR-LF, LF, CR of a token all to LF
   --  @Parameter:
   --    The stringvalue of a token
   --  @Returns:
   --    Same string as given, but with only LFs as line ends
   function Unify_Line_Ends
     (Token_Value : ASU.Unbounded_String)
      return ASU.Unbounded_String;

private

   --  The parser is modelled by a finite automaton, which stores all
   --  symbols (e.g. the name of a variable). At functions and procedures,
   --  it also stores all types of its parameters (in the right order).
   type Parser_State_Type is
     (WAITING_FOR_SYMBOL,
      WAITING_FOR_DOT,
      WAITING_FOR_ANNEX,
      WAITING_FOR_PROCEDURE_OR_FUNCTION_NAME,
      WAITING_FOR_COLON,
      WAITING_FOR_PARAMETERTYPE,
      WAITING_FOR_PARAM_ANNEX,
      WAITING_FOR_PACKAGE_NAME,
      WAITING_FOR_IS_OR_RENAMES,
      WAITING_FOR_NEW,
      IGNORE_ALL_UNTIL_SEMICOLON);
   Parser_State : Parser_State_Type := WAITING_FOR_SYMBOL;

   Current_Filepath              : ASU.Unbounded_String;
   Current_Packagename           : ASU.Unbounded_String;
   Current_Packagename_Candidate : ASU.Unbounded_String;
   Current_Symbol                : Symbol.Symbol_Type;

end Code_Parser;
